
function getUserAddress(user){
    //获取后台信息
    $.ajax({
        url:"easybuy/getUserAddress",
        dataType:"json",
        async:false,
        data:{userId:user.id},
        success:function(UserAddressList) {
            $(".address").empty();
            var $table = $(".address");
            for (var i = 0;i<UserAddressList.length;i++) {
                if (user != null && user.id == UserAddressList[i].userId) {
                    for (var i in UserAddressList) {
                        var is="";
                        if (UserAddressList[i].isDefault == 1) {
                            is='isDefault';
                        }
                        var de;
                        if (UserAddressList[i].isDefault==1){
                            de="<span class='w1'>(默认地址)</span>"
                        }else {
                            de="<span class='w1'><a onclick='dianji("+UserAddressList[i].id+")'>设置为默认地址</a></span>"
                        };

                        $($table).append(" <table onclick='modify("+UserAddressList[i].id+")' class='address_table "+is+"'>\n" +
                            "                        <tr>\n" +
                            "                            <td colspan=\"2\" style=\"font-size:14px; color:#ff4e00;\">" + UserAddressList[i].remark +de+
                            "<td class=\"a_close\">" +
                            "<a onclick='deleAddress("+UserAddressList[i].id+")'>\n" +
                            "<img src=\"images/a_close.png\">\n" +
                            "</a>\n" +
                            "</td>" +
                            "                        </tr>\n" +
                            "                        <tr>\n" +
                            "                            <td align=\"right\" width=\"80\">收货人姓名：</td>\n" +
                            "                            <td>" + UserAddressList[i].userName + "</td>\n" +
                            "                        </tr>\n" +
                            "                        <tr>\n" +
                            "                            <td align=\"right\">手机号码：</td>\n" +
                            "                            <td>" + UserAddressList[i].mobile + "</td>\n" +
                            "                        </tr>\n" +
                            "                        <tr>\n" +
                            "                            <td align=\"right\">配送地址：</td>\n" +
                            "                            <td>" + UserAddressList[i].address + "</td>\n" +
                            "                        </tr>\n" +
                            "                        <tr>\n" +
                            "                            <td align=\"right\">备注：</td>\n" +
                            "                            <td>" + UserAddressList[i].remark + "</td>\n" +
                            "                        </tr>\n" +
                            "                        <tr>\n" +
                            "                            <td align=\"right\">创建时间：</td>\n" +
                            "                            <td>" + UserAddressList[i].createTime + "</td>\n" +
                            "                        </tr>\n" +
                            "                    </table>")
                    }
                }
            }
            $(".address_table").css("cursor","pointer");
            $(".address_table").click(function () {
                $(this)
            });
        }
    })
}
//点击切换修改窗口
function modify(id) {
    $.ajax({
        url:"easybuy/getAddress",
        dataType: "json",
        type:"post",
        data:{
            id:id
        },
        success:function (data) {
            $("#addAddress").hide();
            $("#modifyAddress").show();
            $("#modifyAddress [name='address']").val(data.address);
            $("#modifyAddress [name='remark']").val(data.remark);
            if (data.isDefault==1){
                $("#modifyAddress [name='isDefault']")[0].checked=true;
            }else {
                $("#modifyAddress [name='isDefault']")[0].checked=false;
            }
            $("#modifyAddress p a:eq(0)").attr('onclick','deleAddress('+data.id+')');
            $("#modifyAddress p a:eq(1)").attr('onclick','Modify('+data.id+')');
        }
    })
}
//修改
function Modify(id) {
    var user=JSON.parse(sessionStorage.getItem("user"));
    var address=$("#modifyAddress [name='address']").val();
    var remark=$("#modifyAddress [name='remark']").val();
    var isDefault;
    if ($("#modifyAddress [name='isDefault']")[0].checked){
        isDefault=1;
    }else {
        isDefault=0;
    }
    if (address==""){
        alert("地址不能为空！");
    } else if (remark == "") {
        alert("备注不能为空！");
    }else {
        $.ajax({
            url:"easybuy/modifyUserAddress",
            type:"post",
            data:{
                address:address,
                remark:remark,
                isDefault:isDefault,
                userId:user.id,
                id:id
            },
            success:function (data) {
                if(data=="true"){
                    alert("修改成功");
                    var user=JSON.parse(sessionStorage.getItem("user"));
                    getUserAddress(user);
                }else{
                    alert("修改失败");
                }
            }
        })
    }
}
//删除
function deleAddress(id) {
    if (confirm("是否删除该地址？")){
        $.ajax({
            url:"easybuy/deleAddress",
            data:{
                id:id
            },
            success:function (data) {
                if(data=="true"){
                    alert("删除成功");
                    var user=JSON.parse(sessionStorage.getItem("user"));
                    getUserAddress(user);
                }else{
                    alert("删除失败");
                }
            }
        })
    }

}
//添加地址
function addAddress(){
    var user=JSON.parse(sessionStorage.getItem("user"));
    var address=$("#province").val()+$("#city").val()+$("#addAddress [name='address']").val();
    var remark=$("#addAddress [name='remark']").val();
    var createTime=new Date();
    var isDefault;
    if ($("#addAddress [name='isDefault']")[0].checked) {
        isDefault=1;
    }else isDefault=0;
    if ($("#addAddress [name='address']").val()==""){
        alert("详细地址不能为空！");
    } else if (remark == "") {
        alert("备注不能为空！");
    }else {
        $.ajax({
            url:"easybuy/addUserAddress",
            data:{
                userId:user.id,
                address:address,
                remark:remark,
                createTime:createTime,
                isDefault:isDefault
            },
            success:function (data) {
                if(data=='true'){
                    alert("添加成功");
                    getUserAddress(user);
                }else {
                    alert("添加失败")
                }
            }
        });
    }
}
function addShow(){
    $("#modifyAddress").hide();
    $("#addAddress").show();
}

function dianji(id) {
    var user=JSON.parse(sessionStorage.getItem("user"));
    $.ajax({
        url:"easybuy/isDefaultUserAddress",
        type:"post",
        data: {
            id:id,
            userId: user.id
        },
        success:function (data) {
            if(data=="false"){
                alert("设置失败！")
            }else{
                getUserAddress(user);
            }
        }
    });
}
$(function () {
    var user=JSON.parse(sessionStorage.getItem("user"));
    getUserAddress(user);

});